package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.cloud.OSSFactory;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.JxCourseEntity;
import io.renren.entity.JxCourseTypeEntity;
import io.renren.form.DescForm;
import io.renren.service.JxCourseService;
import io.renren.service.JxCourseTypeService;
import io.renren.service.JxStudyFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有关于课程的接口
 */
@RestController
@RequestMapping("/api/jxCourse")
@Api(tags="课程")
public class ApiJxCourseController {
    @Autowired
    private JxCourseTypeService jxCourseTypeService;

    @Autowired
    private JxCourseService jxCourseService;

    @Autowired
    private JxStudyFileService jxStudyFileService;

    @PostMapping("courseTypes")
    @ApiOperation("课程分类列表")
    public R courseTypeList(){
        Map<String,Object> params=new HashMap<String,Object>();
        Integer maxLevel=jxCourseTypeService.getMaxLevel();
        params.put("level",maxLevel);
        List<JxCourseTypeEntity> list= jxCourseTypeService.getListByPo(params);
        return R.ok().put("data",list);
    }


    @PostMapping("hotCourse")
    @ApiOperation("热门课程列表")
    public R hotCourseList(@RequestParam Map<String,Object> params){
        params.put("isHot","1");
        PageUtils page=jxCourseService.getAllWithPage(params);
        return R.ok().put("data",page);
    }

    @PostMapping("courseList")
    @ApiOperation("课程列表")
    public R courseList(@RequestParam Map<String,Object> params){
        PageUtils page=jxCourseService.getAllWithPage(params);
        return R.ok().put("data",page);
    }


    @PostMapping("desc")
    @ApiOperation("课程详情")
    public R courseDesc(@RequestBody DescForm form){
        JxCourseEntity  entity =jxCourseService.getById(form.getId());
        return R.ok().put("data",entity);
    }


    @PostMapping("files")
    @ApiOperation("课程资料")
    public R courseFiles(@RequestParam Map<String,Object> params){
        PageUtils page=jxStudyFileService.getAllWithPage(params);
        return R.ok().put("data",page);
    }
    /*@Login
    @ApiOperation("下载文件")
    @RequestMapping(value = "/downFile",method = RequestMethod.GET)
    public R downModel(HttpServletResponse response, HttpServletRequest request) {
        String fileUrl = request.getParameter("fileUrl");
        String path="studyFile";
        try{
            String re= OSSFactory.build().downLoad(response,fileUrl,path);
            if(re.length()>0){
                return R.ok().put("msg","已经下载到"+re);
            }else{
                return  R.error("数据异常");
            }
        }catch (Exception e){
            return  R.error("数据异常");
        }
    }*/
    @Login
    @ApiOperation("下载文件")
    @RequestMapping(value = "/downFile",method = RequestMethod.GET)
    public R downModel(HttpServletResponse response, HttpServletRequest request) {
        String fileUrl = request.getParameter("fileUrl");
        String path=request.getParameter("path");

        String localPath[]=fileUrl.split("\\/");
        int len=localPath.length;
        String fileName = localPath[len-1];
        ServletOutputStream out;
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        try{
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else {
                // 非IE浏览器的处理：
                fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            OSSFactory.build().downLoad(response,fileUrl,path);

        }catch (Exception e){
            return  R.error("数据异常");
        }
        return null;
    }

}
